<?php
require_once('includes/common.php');
/////////// Collect Paging Data ////////////
$offset = 4; // used on paging
$msg = '';

pr($_POST);
///////////// FOR DELETE ///////////////////
if( isset( $_GET['mode'] ) && ($_GET['mode'] == 'delete' ) ) {
  $id = $_GET['id'];
  $delQuery = "DELETE FROM `users` WHERE `id`='$id'";
  mysql_query($delQuery);
  $msg = "Total ".mysql_affected_rows()." user deleted.";
}
///////////////////////////////////////////
/////////////// FOR SORT ///////////////////////

  $sortField = isset( $_GET['field'] ) ? $_GET['field'] : 'uname';
  $sortOrder = isset( $_GET['order'] ) ? $_GET['order'] : 'asc';

  $orderBy = " ORDER BY $sortField $sortOrder ";

///////////////FOR SEARCH ///////////////////////

$searchText = !empty($_POST['search']) ? trim($_POST['search']) : '';
if($searchText != ''){
  $where = " WHERE `uname` LIKE '%$searchText%' ";
} else {
  $where = '';
}
//////////////////////////////////////
$resultTotal = fetchAll('users',$where,$orderBy,'','COUNT(*) AS `total`');
$ans = mysql_fetch_assoc($resultTotal);
$totalRecords = $ans['total'];

$pageNo = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = (($pageNo - 1 ) * $offset);
$totalPages = ceil($totalRecords/$offset);

$result = fetchAll('users',$where,$orderBy,"LIMIT $limit,$offset");

//////////////////////////////////////////////
?>
 <link href="./css/bootstrap.css" rel="stylesheet"/>

<!----Success or warning message-->
<?php echo $msg; ?>

<!---Search Form Start --->
<form class="form-search" id="searchFormId">
  <div class="input-append">
    <input type="text" name="search" style="height:30px;width:200px;" class="span2 search-query">
    <button type="submit" onClick="submitForm();" class="btn">Search</button>
  </div>
</form>
<!------------------------>

<h4>Total Records : <em><?=$totalRecords?></em></h4>

<script>
  function submitForm(){
    oFrm = document.getElementById('searchFormId');
    oFrm.action = '';
    oFrm.method = 'post';
    oFrm.submit();
  }
</script>

 <table class="table table-hover">
    <thead>
      <tr>
        <th>id</th>
        <th>
          <a href="show_all_users.php?order=asc&field=uname"><i class="icon-arrow-up"></i></a>
          <a href="show_all_users.php?order=desc&field=uname"><i class="icon-arrow-down"></i></a>
          uname</th>
        <th>email</th>
        <th>avatar</th>
        <th>added_date</th>
        <th>status</th>
        <th>Options</th>
      </tr>
    </thead>
    <tbody>
 <?php
while($ans = mysql_fetch_assoc($result)){
?>
      <tr>
        <td><a href="view_profile.php?id=<?=$ans['id']?>"><?=$ans['id']?></a></td>
        <td><?=$ans['uname']?></td>
        <td><a href="mailto:<?=$ans['email']?>"><?=$ans['email']?></a></td>
        <td><img src="uploads/<?=$ans['avatar']?>" height="90" width="120" ></td>
        <td><?=date('d-m-Y H:i:s a',strtotime($ans['added_date']));?></td>
        <td><?php

        echo ($ans['status']==1)?'Active':'Inactive';

         ?></td>
        <td><a target="_blank" href="update_user.php?id=<?=$ans['id']?>"><i class="icon-edit"></i></a>

        <a href="show_all_users.php?mode=delete&id=<?=$ans['id']?>"><i onClick="return confirm('Are you sure to delete?')"class="icon-trash"></i>

        </td>

      </tr>
<?php
}
?>
    </tbody>
   <tr>
     <td></td>
   </tr>
 </table>

<div class="pagination pagination-large pagination-centered">
  <ul>
<?php
$isFirstPage = ($pageNo == 1) ? 'active':'';
$pHref = ($pageNo != 1)?"show_all_users.php?page=".($pageNo-1):'#';
?>
    <li class="<?=$isFirstPage?>"><a href="<?=$pHref?>"> Prev</a></li>

<?php
for ($i=1 ; $i<=$totalPages ; $i++ ) {

  $isActive = ($i == $pageNo) ? 'active':'';


?>
    <li class="<?=$isActive?>"><a  href="show_all_users.php?page=<?=$i?>"><?=$i?></a></li>
<?php
}
?>
    <li><a href="show_all_users.php?page=<?=($pageNo+1)?>"> Next </a></li>
  </ul>
 </div>
